import datetime
from flask import current_app
from flask import g
from flask import render_template
from flask import request
from flask import Flask,jsonify
from flask import send_file

from info.utils.common import user_login
from info.utils.response_code import RET,error_map
from info.models import  House, Area, Order
from . import house_blu


@house_blu.route("/favicon.ico")
def favicon():
    return send_file("static/favicon.ico")


@house_blu.route("/api/v1.0/session")
@user_login
def session():
    user = g.user

    if not user:
        return jsonify(errno=RET.SESSIONERR, errmsg="用户未登录")

    data = {
        "user_id": user.id,
        "name": user.name
    }

    return jsonify(errno=RET.OK, errmsg="OK", data=data)


@house_blu.route("/api/v1.0/areas")
def areas():
    areas= []
    try:
        areas = Area.query.filter().all()
    except Exception as e:
        current_app.logger.error(e)
    areas_li = [area.to_dict() for area in areas]

    return jsonify(data=areas_li, error="成功",errno=RET.OK)


@house_blu.route('/api/v1.0/houses/index')
def index1():

    houses = []
    try:
        houses = House.query.all()
    except Exception as e:
        current_app.logger.error(e)
    houses_li=[house.to_basic_dict() for house in houses]
    return jsonify(data=houses_li, error="成功", errno=RET.OK)


@house_blu.route("/")
def index():
    return send_file("static/html/index.html")














